package com.lihh;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class ZooKeeperDemo001 {
  // 集群地址
  private String connectString = "192.168.56.17:2181,192.168.56.18:2181,192.168.56.19:2181";
  // 连接 延迟地址
  private int sessionTimeout = 2000;
  
  // zk 连接客户端
  private ZkClient zkClient;
  
  @Before
  public void before() {
    zkClient = new ZkClient(connectString, sessionTimeout, 1000 * 15, new SerializableSerializer());
  }
  
  /**
   * 创建 节点
   *
   * @author lihh
   */
  @Test
  public void createData() {
    String appData = zkClient.create("/node01", "lihh001", CreateMode.PERSISTENT);
    System.out.println(appData);
    
    while (true) {
    }
  }
  
  /**
   * 进行 数据的编辑
   *
   * @author lihh
   */
  @Test
  public void modifyData() {
    zkClient.writeData("/node01", "lihh02");
    
    while (true) {
    }
  }
  
  /**
   * 拿到所有的子元素
   *
   * @author lihh
   */
  @Test
  public void getChildren() {
    List<String> children = zkClient.getChildren("/node01");
    children.forEach(System.out::println);
    
    while (true) {
    }
  }
  
  @After
  public void after() {
    zkClient.close();
  }
}
